<?php

namespace App\Http\Controllers;

use App\Constants\ResponseCodeConstant;
use App\Http\Requests\ResourceCreateRequest;
use App\Services\AdminResourceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 后台资源管理
 * Class AdminUserController
 * @package App\Http\Controllers
 */
class AdminResourceController extends BaseController
{

    /**
     * 添加后台资源
     * @param ResourceCreateRequest $request
     * @return JsonResponse
     */
    public function create(ResourceCreateRequest $request): JsonResponse
    {
        $name = $request->post('name');
        $url = $request->post('url');
        $description = $request->post('description');
        $category_id = $request->post('category_id');

        $data = [
          'name' => $name,
          'url' => $url,
          'description' => $description,
          'category_id' => $category_id
        ];

        $result = AdminResourceService::getInstance()->create($data);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::CREATE_FAILED,'资源创建失败');
    }

    /**
     * 修改后台资源
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function update(Request $request)
    {
        $id = $request->route('id');
        $data = [];
        if ($request->has('name')) {
            $data['name'] = $request->post('name');
        }
        if ($request->has('url')) {
            $data['url'] = $request->post('url');
        }
        if ($request->has('description')) {
            $data['description'] = $request->post('description');
        }
        if ($request->has('category_id')) {
            $data['category_id'] = $request->post('category_id');
        }
        $result = false;
        if ($data) {
            $result = AdminResourceService::getInstance()->updateById($data,$id);
        }
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'资源修改失败');
    }

    /**
     * 根据ID获取资源详情
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function getById(Request $request)
    {
        $id = $request->route('id');
        $result = AdminResourceService::getInstance()->getById($id);
        if ($result) {
            return $this->success($result);
        }
        return $this->fail(ResponseCodeConstant::SEARCH_FAILED,'资源信息查找失败');
    }

    /**
     * 根据ID删除后台资源
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        $result = AdminResourceService::getInstance()->deleteById($id);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::DELETE_FAILED,'资源信息删除失败');
    }

    /**
     * 分页模糊查询后台资源
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $category_id = $request->get('category_id',0);
        $name = $request->get('name','');
        $url = $request->get('url','');
        $pageSize = $request->get('pageSize',15);
        $pageNum = $request->get('pageNum',1);
        $data = AdminResourceService::getInstance()->list($category_id,$name,$url,$pageSize,$pageNum);
        $result = $this->successPagniate($data);
        return $this->success($result);
    }

    /**
     * 查询所有后台资源
     * @return JsonResponse|mixed|object
     */
    public function listAll()
    {
        $result = AdminResourceService::getInstance()->listAll();
        if ($result) {
            return $this->success($result);
        }
        return $this->fail(ResponseCodeConstant::SEARCH_FAILED,'资源信息查询失败');
    }

}
